from threading import Thread
from selenium import webdriver
from base.base_analyze import mysql_file


class InitDriver():
    # 定义类属性保存驱动对象
    # 定义标记,判断是否需要退出驱动对象
    _driver = None

    # 定义标记,判断是否需要退出驱动对象
    _auto_quit = True

    # 获取浏览器驱动对象  类方法
    @classmethod
    def get_drivers( cls, brower):
        # 判断_driver中是否存有驱动,如果没有实例化一个保存在_driver中
        # 返回_driver中的驱动对象
        if cls._driver is None:
            if brower == 'chrome':
                cls._driver = webdriver.Chrome()
            elif brower == 'firefox':
                cls._driver = webdriver.Firefox()
            elif brower == "ie":
                cls._driver = webdriver.Ie()
            cls._driver.maximize_window()
            cls._driver.implicitly_wait(20)
        return cls._driver

    @classmethod
    def get_driver(cls):
        browers = mysql_file("brower.yaml", "brower")
        print(browers)
        threads = []
        files = range(len(browers))
        for brower in browers:
            t = Thread(target=cls.get_drivers, args=(brower,))
            threads.append(t)
        for t in files:
            threads[t].start()
        for t in files:
            threads[t].join()
        return cls._driver



    # 退出浏览器驱动对象 类方法
    @classmethod
    def quit_driver(cls):
        # 如果_driver属性中有驱动对象,退出驱动对象-重置为None
        if cls._auto_quit and cls._driver is not None:
            cls._driver.quit()
            cls._driver = None
    # 手动设置自动退出
    @classmethod
    def set_auto_quit(cls, auto_quit):
        cls._auto_quit = auto_quit


